package top.italks.manufactureproject.ui.post;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.GridView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import top.italks.manufactureproject.R;
import top.italks.manufactureproject.constant.Config;
import top.italks.manufactureproject.ui.post.adapter.PostAdapter;
import top.italks.manufactureproject.ui.post.adapter.PostAdapter.OnItemClickListener;
import top.italks.manufactureproject.ui.post.bean.JobPeopleInfo;
import top.italks.manufactureproject.ui.post.bean.PeopleGetInfo;
import top.italks.manufactureproject.ui.post.bean.UserPeopleSearch;
import top.italks.manufactureproject.ui.post.bean.UserProductionLineGetAll;
import top.italks.manufactureproject.utils.HttpUtils;

public class PostActivity extends AppCompatActivity {

    @BindView(R.id.gridView)
    GridView gridView;

    private PostAdapter adapter;

    private List<UserProductionLineGetAll.DataBean> userProductionList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);
        ButterKnife.bind(this);
        adapter = new PostAdapter(this);
        gridView.setAdapter(adapter);
        gridView.setHorizontalSpacing(10);
//        gridView.setVerticalSpacing(20);
        adapter.setListener(new OnItemClickListener() {
            @Override
            public void Num1Click(int position, TextView name, TextView value) {
                //工程师adapter
                Log.e("Num1Click", "工程师");
            }

            @Override
            public void Num2Click(int position, TextView name, TextView value) {
//技术员
                Log.e("Num2Click", "技术员");
                for (JobPeopleInfo info : adapter.getList().get(position).getUnList()) {
                    if (info.getStatus() == 1) {
//                        name.setText(String.format("姓名:%s", info.getPeopleName()));
//                        value.setText("体力:" + info.getHp());
                        adapter.getList().get(position).getList().add(info);
                        adapter.getList().get(position).getUnList().remove(info);
                        break;
                    }
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void Num3Click(int position, TextView name, TextView value) {
//操作员
                Log.e("Num3Click", "操作员");
            }

            @Override
            public void Num4Click(int position, TextView name, TextView value) {
//质检员
                Log.e("Num4Click", "质检员");
                for (JobPeopleInfo info : adapter.getList().get(position).getUnList()) {
                    if (info.getStatus() == 3) {
                        adapter.getList().get(position).getList().add(info);
                        adapter.getList().get(position).getUnList().remove(info);
                    }
                }
                adapter.notifyDataSetChanged();
            }
        });
        getAll();


    }

//    private void getStatusPeople(int status){
//        //获取某类型的人员
//    }

    //1、查询生产线信息
    private void getAll() {
        HttpUtils.post(Config.url + "/dataInterface/UserProductionLine/getAll", null,
            new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String json = response.body().string();
                    UserProductionLineGetAll bean = new Gson()
                        .fromJson(json, UserProductionLineGetAll.class);
                    userProductionList = bean.getData();
                    adapter.setList(bean.getData());

                    if (bean.getData().size() > 0) {
                        for (UserProductionLineGetAll.DataBean info : bean.getData()) {
                            userPeopleSearch(info.getId());
                        }
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();
                        }
                    });
                }
            });
    }

    //2，查询生产线上人员
    private void userPeopleSearch(final int userProductionLineId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("userProductionLineId", userProductionLineId);
        HttpUtils.post(Config.url + "/dataInterface/UserPeople/search"
            , map, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String json = response.body().string();
                    UserPeopleSearch bean = new Gson()
                        .fromJson(json, UserPeopleSearch.class);
                    //筛选出岗位上的人
//                    userProductionList
                    List<JobPeopleInfo> peopleList = new ArrayList<>();
                    List<JobPeopleInfo> unPeopleList = new ArrayList<>();

                    for (UserPeopleSearch.DataBean info : bean.getData()) {
                        if (!TextUtils.isEmpty(info.getWorkPostId())) {
                            //已经在生产线上的员工
                            JobPeopleInfo peopleInfo = new JobPeopleInfo();
                            peopleInfo.setUserWorkId(info.getUserWorkId());
                            peopleInfo.setUserProductionLineId(info.getUserProductionLineId());
                            peopleInfo.setPeopleId(info.getPeopleId());
                            peopleInfo.setWorkPostId(info.getWorkPostId());
                            //查询人员的信息
                            peopleGetInfo(info.getPeopleId());
                            peopleList.add(peopleInfo);
                        } else {
                            //未在生产线上的员工
                            JobPeopleInfo peopleInfo = new JobPeopleInfo();
                            peopleInfo.setUserWorkId(info.getUserWorkId());
                            peopleInfo.setUserProductionLineId(info.getUserProductionLineId());
                            peopleInfo.setPeopleId(info.getPeopleId());
                            peopleInfo.setWorkPostId(info.getWorkPostId());
                            //查询人员的信息
                            peopleGetInfo(info.getPeopleId());
                            unPeopleList.add(peopleInfo);
                        }
                    }
                    for (UserProductionLineGetAll.DataBean info : userProductionList) {
                        //TODO 判断学生生产线ID，一定要区别与生产线ID
                        if (info.getId() == userProductionLineId) {
                            info.setList(peopleList);
                            info.setUnList(unPeopleList);
                        }
                    }


                }
            });
    }

    //3、根据员工ID，查询员工信息
    private void peopleGetInfo(final int id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        HttpUtils.post(Config.url + "/dataInterface/People/getInfo"
            , map, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String json = response.body().string();
                    PeopleGetInfo bean = new Gson()
                        .fromJson(json, PeopleGetInfo.class);

                    for (UserProductionLineGetAll.DataBean info : userProductionList) {
                        for (JobPeopleInfo jobInfo : info.getList()) {
                            if (jobInfo.getPeopleId() == id) {
                                jobInfo.setPeopleName(bean.getData().get(0).getPeopleName());
                                jobInfo.setHp(bean.getData().get(0).getHp());
                                jobInfo.setStatus(bean.getData().get(0).getStatus());
                            }
                        }
                        for (JobPeopleInfo jobInfo : info.getUnList()) {
                            if (jobInfo.getPeopleId() == id) {
                                jobInfo.setPeopleName(bean.getData().get(0).getPeopleName());
                                jobInfo.setHp(bean.getData().get(0).getHp());
                                jobInfo.setStatus(bean.getData().get(0).getStatus());
                            }
                        }
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();
                        }
                    });
                }
            });
    }
}
